package com.fingard.dsp.bank.directbank.ceb02;

import com.fingard.dsp.bank.directbank.bankInterface.ICertificateMailedMsg;
import com.fingard.dsp.bank.reqrespobj.certificatemailmsg.ReqCertificateMailedMsgQry;
import com.fingard.dsp.bank.reqrespobj.certificatemailmsg.ReqCertificateMailedMsgQryDetail;
import com.fingard.dsp.bank.reqrespobj.certificatemailmsg.RetAtsCertificateMailedMsg;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CEB02CertificateMailedMsg extends CEB02Base implements ICertificateMailedMsg{

    @Override
    public RetAtsCertificateMailedMsg getCertificateMailedMsg(ReqCertificateMailedMsgQry p_orderMsg) throws Exception {
    	RetAtsCertificateMailedMsg retAts = new RetAtsCertificateMailedMsg(p_orderMsg.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        int pageNum = 50;
        int sendNum = 0;
        int m = 0;
        try {
        	int total = Integer.valueOf(p_orderMsg.Z_BISHU);
            //每次推送50条
            int pageSize = total/pageNum;
        	while (m < pageSize + 1){
            CEB02Header bankHeader = new CEB02Header();
            //银行获取相关信息
            bankHeader.FTranCode = "Q058";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader_3(xmlWriter, bankHeader);          
            //写入body           
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeElementString("CHANNEL_CODE", bankFront.org);//接入机构号
            xmlWriter.writeElementString("DEALER_CODE",p_orderMsg.DealerCode );//接入机构号 bankFront.corpNo
            xmlWriter.writeElementString("DEALER_NAME",p_orderMsg.DealerName);// 接入机构名称
            xmlWriter.writeElementString("MAILING_NUMBER", p_orderMsg.MailNo);//邮寄单号  p_orderMsg.MAILING_NUMBER
            xmlWriter.writeElementString("EXPRESS_COMPANY_NAME", p_orderMsg.MailCompanyName);//快递公司名称  p_orderMsg.EXPRESS_COMPANY_NAME
            xmlWriter.writeElementString("MAILING_TIME", p_orderMsg.MailTime);//邮寄时间 p_orderMsg.MAILING_TIME
            while (sendNum < pageNum && p_orderMsg.readRecord()) {
                ReqCertificateMailedMsgQryDetail tmpDetail = p_orderMsg.reqDetail;
                sendNum++;
                XElement tmpXElement = new XElement("Frame", "");
                tmpXElement.addChild(new XElement("VEHICLE_CODE", tmpDetail.VIN));
                tmpXElement.addChild(new XElement("CERTIFICATE", tmpDetail.CertificationNo));
                tmpXElement.addChild(new XElement("ORDER_NUMBER", tmpDetail.OrderCode));
                xmlWriter.writeXElement(tmpXElement);
            }
            xmlWriter.writeElementString("MAILING_NUM",sendNum+"");//邮寄数量 p_orderMsg.MAILING_NUM
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            System.out.println(xmlWriter.toXmlString());
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);//发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/Out/Head/ErrCode");
                    String tmpMsg = tmpBkReader.getFirstText("/Out/Body/ErrorInfo");
                    if (tmpRetCode.length() == 0) {
                        while (tmpBkReader.read("/Out/Body")) {
                            String tmpReturnCode = tmpBkReader.readedXElement.getSingleTextNotNull("RESULT_FLAG");
                            String tmpReturnMsg = tmpBkReader.readedXElement.getSingleTextNotNull("MSG");
                            if ("1".equals(tmpReturnCode)) {
                                tmpRespCode = "0";
                                tmpRespInfo = "操作成功";
                            } else {
                                tmpRespCode = "-1";
                                tmpRespInfo = tmpReturnCode + " : " + tmpReturnMsg;
                            }
                        }                       
                        m++;
                        //判断是否恰好满一次
                        if(sendNum*m == total) {
                        	break;
                        }else {
                        	sendNum = 0;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpRetCode + ":" + tmpMsg;
                        break;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                    break;
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
                break;
            }
         }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }


}
